package com.apes.fn.server.workOrder.repository;


import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.fn.server.workOrder.model.MaintenanceOrder;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * param:zyc
 **/
public interface MaintenanceOrderRepository extends CustomRepository<MaintenanceOrder, String> {
    @Query(value = " SELECT S FROM MaintenanceOrder S  "
                 + " WHERE S.lastUpdatedStamp > :times OR (S.id > :id AND S.lastUpdatedStamp = :times) ORDER BY S.lastUpdatedStamp, S.id ASC "
    )
    List<MaintenanceOrder> synMaintenanceOrder(@Param("times") Date times, @Param("id") String id, Pageable pageable);

    @Query(value = " SELECT S FROM MaintenanceOrder S  "
            + " WHERE S.dispatcheState = :state AND S.servicePlanItem.id in :list "
    )
    List<MaintenanceOrder> findOutDiapatcherOrder(@Param("state") String state, @Param("list") List<String> list);

    @Query(value = " SELECT S FROM MaintenanceOrder S  "
            + " WHERE S.businessType = 'out' AND S.state = :state AND S.servicePlanItem.id in :list "
    )
    List<MaintenanceOrder> findOutMaintenanceOrder(@Param("state") String state,@Param("list") List<String> list);

    @Query(value = " SELECT S FROM MaintenanceOrder S  "
            + " WHERE S.businessType = 'common' AND S.servicePlanItem.id in :list "
    )
    List<MaintenanceOrder> findCommonMaintenanceOrder(@Param("list") List<String> list);

    //MaintenanceOrder findFirstByServicePlanIdAndBusinessTypeNotIn(String servicePlanId, List<String> businessType);


    MaintenanceOrder findFirstByServicePlanItemAndBusinessTypeNotIn(ServicePlanItem servicePlanItem, List<String> businessType);

    MaintenanceOrder findFirstByServicePlanItemIdAndBusinessTypeNotIn(String servicePlanItemId, List<String> businessType);

    @Query(value = "select o.INNER_DEPT_ID dept_id,p.refer_tax referTax ,(SELECT d.name from mas_dept d where d.id=o.INNER_DEPT_ID) dept_name from ser_maintenance_order o left join ser_SERVICE_PLAN_ITEM p on p.id = o.SERVICE_PLAN_ITEM" +
            " where o.INNER_DEPT_ID is not null and o.BELONG_DEPT_ID = :deptId and o.PRODUCT_ID=:productId and o.CREATE_DATE>(sysdate-90)",nativeQuery = true)
    List<Map> findInnerMaintenanceOrderByDept(@Param("deptId") String deptId , @Param("productId") String productId);

    @Query(value = " SELECT S FROM MaintenanceOrder S  "
            + " WHERE S.belongDept.id = :deptId AND S.fnCustomerExpand.id = :fnCustomerExpandId AND S.servicePlanItem.assistType.id = 'ServicePlanItem.assistType.normal' "
            +" AND S.lockState = false AND S.workPlatform in ('cosmetology','machineRepa')"
    )
    List<MaintenanceOrder> findMaintenanceOrderForWork(@Param("deptId") String dept,@Param("fnCustomerExpandId") String fnCustomerExpand);

    @Query(value = " SELECT S FROM MaintenanceOrder S  "
            + " WHERE S.createDate >= :startDate and S.createDate < :endDate "
    )
    List<MaintenanceOrder> findMaintenanceWorkderForHistory(@Param("startDate") Date start,@Param("endDate") Date end);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.id in :list ")
    List<MaintenanceOrder> findInnerMaintenanceOrder(@Param("list") List<String> list);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.id in :list ")
    List<MaintenanceOrder> findMaintenanceOrderByIdList(@Param("list") List<String> list);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.sourceFrom = :sourceId and S.businessType = 'out' and S.state in ('create','finish') ")
    List<MaintenanceOrder> findOutOrderByCommonOrder(@Param("sourceId") String sourceOrderId);


    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.servicePlan.id = :planId and S.businessType = 'out' and S.state in ('finish') ")
    List<MaintenanceOrder> findOutOrderByServicePlan(@Param("planId") String planId);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.servicePlan.id = :planId and S.businessType in :businessType and S.workState = :workState and S.state <> 'termination'")
    List<MaintenanceOrder> findWorkStateByServicePlan(@Param("planId") String planId,@Param("workState") String workState,@Param("businessType") List businessType);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.servicePlan.id = :planId and S.businessType = 'common' and S.workStartTime is not null")
    List<MaintenanceOrder> findWorkindByServicePlan(@Param("planId") String planId);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.servicePlan.id = :planId and S.businessType in :businessType and S.state <> 'termination'")
    List<MaintenanceOrder> findByServicePlanAndBusinessType(@Param("planId") String planId,@Param("businessType") List businessType);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.id = :servicePlanItem and S.businessType in :businessType and S.state not in ('back','termination')")
    List<MaintenanceOrder> findByServicePlanItemAndBusinessType(@Param("servicePlanItem") String servicePlanItem,@Param("businessType") List businessType);

    MaintenanceOrder findByServicePlanItem(ServicePlanItem servicePlanItem);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.servicePlan.id = :planId and S.product.productGroup.id = '10'")
    List<MaintenanceOrder> findFittingByServicePlanId(@Param("planId") String planId);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.id IN (:servicePlanItemIds)")
    List<MaintenanceOrder> findMaintenanceOrderByServicePlanItemIds(@Param("servicePlanItemIds") List servicePlanItemIds);

    MaintenanceOrder findByServicePlanItemAndBusinessType(ServicePlanItem servicePlanItem,String businessType);

    @Query(value = " SELECT S FROM MaintenanceOrder S  "
            + " WHERE S.businessType = :businessType AND S.servicePlanItem.id in :list "
    )
    List<MaintenanceOrder> findMaintenanceOrderByOut(@Param("businessType") String businessType, @Param("list") List<String> list);

    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.id = :servicePlanId and S.businessType = :businessType ")
    MaintenanceOrder findByServicePlanItemAndBusinessType(String servicePlanId,String businessType);
    @Query(value = " SELECT S FROM MaintenanceOrder S  WHERE  S.servicePlanItem.id = :servicePlanItem and S.businessType = 'out' and S.state <>'termination' ")
    MaintenanceOrder findByServicePlanItemAndNotTermination(String servicePlanItem);
}
